﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace Xms.Abp.Core;

public interface IDistributedSemaphoreBase
{
    Task<bool> CheckSemaphoreAsync(int millisecondsTimeout = 0, CancellationToken cancellationToken = default);
    Task<bool> CheckSemaphoreAsync(TimeSpan timeout, CancellationToken cancellationToken = default);
    bool CheckSemaphore(int millisecondsTimeout = 0);
    bool CheckSemaphore(TimeSpan timeout);
    Task<bool> ReleaseAsync(int releaseCount = 1, CancellationToken cancellationToken = default);
    bool Release(int releaseCount = 1);

    Task<IDisposable> LockAsync(int releaseCount = 1, int millisecondsTimeout = 0, CancellationToken cancellationToken = default);
    IDisposable Lock(int releaseCount = 1, int millisecondsTimeout = 0);
    Task<IDisposable> LockAsync(TimeSpan timeout, int releaseCount = 1, CancellationToken cancellationToken = default);
    IDisposable Lock(TimeSpan timeout, int releaseCount = 1);
}

public interface IDistributedSemaphore: IDistributedSemaphoreBase
{
    void Initalization(string semaphoreName, int initialCount = 1, string tenantId = null);
    void Dispose(string semaphoreName);
}

public interface IDistributedSemaphore<T>: IDistributedSemaphoreBase, IDisposable
    where T : class
{
    void Initalization(string semaphoreValue = null, int initialCount = 1, string tenantId = null);
}
